package com.chinacoal.microservice.impl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinacoal.microservice.api.UnionTaskMaintainClient;
import com.chinacoal.microservice.api.model.UtaskCompleteTaskParam;
import com.chinacoal.microservice.api.model.entity.UtaskInfoEntity;
import com.chinacoal.microservice.impl.mapper.WorkItemMapper;
import com.chinacoal.microservice.impl.service.TaskPushService;
import com.chinacoal.microservice.model.wofkflow.entity.WorkItemEntity;
import com.chinacoal.microservice.model.wofkflow.resource.WorkflowVar;
import com.chinacoal.microservice.util.result.Result;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class TaskPushServiceImpl implements TaskPushService {

	@Autowired
	private UnionTaskMaintainClient unionTaskMaintainClient;
	
	@Autowired
	private WorkItemMapper workItemMapper;
	
	@Override
	public void taskPush(List<WorkItemEntity> approvalBeforeTaskList, List<WorkItemEntity> approvalAfterTaskList, Long taskId, String opcode) {
		
		List<WorkItemEntity> addTaskList = new ArrayList<WorkItemEntity>();
		List<WorkItemEntity> completeTaskList = new ArrayList<WorkItemEntity>();
		List<WorkItemEntity> cancelTaskList = new ArrayList<WorkItemEntity>();
		
		if(WorkflowVar.APPROVE_AGREED.equals(opcode)) {
			for (WorkItemEntity approvalBeforeTask : approvalBeforeTaskList) {
				if(approvalBeforeTask.getTaskId().equals(taskId)) {
					completeTaskList.add(approvalBeforeTask);
					continue;
				}
				cancelTaskList.add(approvalBeforeTask);
			}
			addTaskList.addAll(approvalAfterTaskList);
		}else if(WorkflowVar.APPROVE_REJECTED.equals(opcode)){
			for (WorkItemEntity approvalBeforeTask : approvalBeforeTaskList) {
				if(approvalBeforeTask.getTaskId().equals(taskId)) {
					completeTaskList.add(approvalBeforeTask);
					continue;
				}
				cancelTaskList.add(approvalBeforeTask);
			}
		}else if(WorkflowVar.APPROVE_RECALL.equals(opcode)){
			cancelTaskList.addAll(approvalBeforeTaskList);
		}
		
		if(completeTaskList.size() > 0) taskPushComplete(completeTaskList);
		if(cancelTaskList.size() > 0) taskPushCancel(cancelTaskList);
		if(addTaskList.size() > 0) taskPushAdd(addTaskList);
	}

	@Override
	public void taskPushAdd(List<WorkItemEntity> taskList) {
		try {
			
			log.info("1、----------------推送待办至待办中心开始--------------");

			List<UtaskInfoEntity> UtaskInfoEntityList = new ArrayList<UtaskInfoEntity>();
			for (WorkItemEntity workItemEntity : taskList) {
				UtaskInfoEntity utaskInfoEntity = new UtaskInfoEntity();
				utaskInfoEntity.setSysCode(workItemEntity.getSysCode());
				utaskInfoEntity.setSendTime(new Date());
				utaskInfoEntity.setSysTaskId(workItemEntity.getTaskId().toString());
				utaskInfoEntity.setTaskName(workItemEntity.getTaskTitle());
				utaskInfoEntity.setSendUserId("");
				utaskInfoEntity.setSendUserName("");
				
				Long userId = workItemMapper.getUserIdByEmpId(workItemEntity.getActivityUser());
				if(userId == null) {
					utaskInfoEntity.setHandleUserId("");
				}else {
					utaskInfoEntity.setHandleUserId(userId.toString());
				}
				
				utaskInfoEntity.setHandleUserName(workItemEntity.getActivityUserName());
				utaskInfoEntity.setPcUrl(workItemEntity.getTaskUrl());
				utaskInfoEntity.setState("0");
				utaskInfoEntity.setLinkCode(workItemEntity.getActivityInsId().toString());
				utaskInfoEntity.setLinkName(workItemEntity.getActivityName());
				utaskInfoEntity.setTypeCode(workItemEntity.getBizCode());
				utaskInfoEntity.setIsInternal("Y");
				utaskInfoEntity.setProcessInstId(workItemEntity.getInstanceId().toString());
				utaskInfoEntity.setBusinessId(workItemEntity.getBusinessId());
				utaskInfoEntity.setBizCode(workItemEntity.getBizCode());
				UtaskInfoEntityList.add(utaskInfoEntity);
			}
			
			Result<Map<String, List<UtaskInfoEntity>>> result = unionTaskMaintainClient.battchTaskAdd(UtaskInfoEntityList);
			if(result != null && result.getCode() == 10000) {
				Map<String, List<UtaskInfoEntity>> resultMap = result.getData();
				List<UtaskInfoEntity> success = resultMap.get("success");
				List<UtaskInfoEntity> error = resultMap.get("error");
				log.info("----------------待办推送成功"+ success.size() +"条--------------");
				log.info("----------------待办推送失败"+ error.size() +"条--------------");
			}else {
				log.info("----------------调用推送接口失败, 原因：" + result.getMsg() + "--------------");
			}
			
			log.info("2、----------------推送待办至待办中心结束--------------");
		} catch (Exception e) {
			log.info("3、----------------调用推送接口异常--------------");
		}
	}

	@Override
	public void taskPushComplete(List<WorkItemEntity> taskList) {
		try {
			log.info("1、----------------推送待办至待办中心开始--------------");
	
			List<UtaskCompleteTaskParam> paramList = new ArrayList<UtaskCompleteTaskParam>();
			for (WorkItemEntity workItemEntity : taskList) {
				UtaskCompleteTaskParam param = new UtaskCompleteTaskParam();
				param.setHandleTime(new Date());
				param.setSysCode(workItemEntity.getSysCode());
				param.setSysTaskId(workItemEntity.getTaskId().toString());
				param.setCompleteFrom("BPS");
				param.setIsInternal("Y");
				paramList.add(param);
			}
			
			Result<Map<String, List<String>>> result = unionTaskMaintainClient.battchCompleteTask(paramList);
			if(result != null && result.getCode() == 10000) {
				Map<String, List<String>> resultMap = result.getData();
				List<String> success = resultMap.get("success");
				List<String> error = resultMap.get("error");
				log.info("----------------待办推送成功"+ success.size() +"条--------------");
				log.info("----------------待办推送失败"+ error.size() +"条--------------");
			}else {
				log.info("----------------调用推送接口失败, 原因：" + result.getMsg() + "--------------");
			}
			
			log.info("2、----------------推送待办至待办中心结束--------------");
		} catch (Exception e) {
			log.info("3、----------------调用推送接口异常--------------");
		}
	}

	@Override
	public void taskPushCancel(List<WorkItemEntity> taskList) {
		try {
			log.info("1、----------------推送待办至待办中心开始--------------");
	
			List<UtaskCompleteTaskParam> paramList = new ArrayList<UtaskCompleteTaskParam>();
			for (WorkItemEntity workItemEntity : taskList) {
				UtaskCompleteTaskParam param = new UtaskCompleteTaskParam();
				param.setHandleTime(new Date());
				param.setSysCode(workItemEntity.getSysCode());
				param.setSysTaskId(workItemEntity.getTaskId().toString());
				param.setCompleteFrom("BPS");
				param.setIsInternal("Y");
				paramList.add(param);
			}
			
			Result<Map<String, List<String>>> result = unionTaskMaintainClient.battchCancelTask(paramList);
			if(result != null && result.getCode() == 10000) {
				Map<String, List<String>> resultMap = result.getData();
				List<String> success = resultMap.get("success");
				List<String> error = resultMap.get("error");
				log.info("----------------待办推送成功"+ success.size() +"条--------------");
				log.info("----------------待办推送失败"+ error.size() +"条--------------");
			}else {
				log.info("----------------调用推送接口失败, 原因：" + result.getMsg() + "--------------");
			}
			
			log.info("2、----------------推送待办至待办中心结束--------------");
		} catch (Exception e) {
			log.info("3、----------------调用推送接口异常--------------");
		}
		
	}

}
